﻿using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Collections.Generic;

using src.Models.Requests;
using src.Models.Responses;
using src.Helpers;

namespace src
{
    public class ApiClient
    {
        public HttpClient Client;
        public static String Token { get; set; } 

        public ApiClient(string baseAddress)
        {
            this.Client = new HttpClient();
            this.Client.BaseAddress = new Uri(baseAddress);
            this.Client.DefaultRequestHeaders.Accept.Clear();
            this.Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        #region User

        public async Task<IResponse> SignUp(UserRequest user)
        {
            var response = await Client.PostAsJsonAsync("api/v1/signup", new { user = user });
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> SignIn(AuthenticateRequest auth)
        {
            var response = await Client.PostAsJsonAsync("/api/v1/signin", new { session = auth });
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        #endregion

        #region Project

        public async Task<IResponse> GetProject(int idProject)
        {
            var response = await Client.GetAsync(ApiUrls.Project(idProject));
            if(response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<ProjectResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> PostProject(ProjectRequest project)
        {
            var response = await Client.PostAsJsonAsync(ApiUrls.Projects(), new { project = project } );
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<ProjectResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IEnumerable<IResponse>> ListProjects()
        {
            var response = await Client.GetAsync(ApiUrls.Projects());
            return await response.Content.ReadAsAsync<List<ProjectResponse>>();
        }

        public async Task<IResponse> DeleteProject(int idProject)
        {
            var response = await Client.DeleteAsync(ApiUrls.Project(idProject));
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> UpdateProject(int idProject, ProjectRequest project)
        {
            var response = await Client.PutAsJsonAsync(ApiUrls.Project(idProject), project);
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        #endregion

        #region Stakeholder

        public async Task<IResponse> GetStakeholder(int idStakeholder)
        {
            var response = await Client.GetAsync(ApiUrls.Stakeholder(idStakeholder));
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<StakeholderResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> PostStakeholder(StakeholderRequest stakeholder)
        {
            var response = await Client.PostAsJsonAsync(ApiUrls.Stakeholders(), new { stakeholder = stakeholder });
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<StakeholderResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IEnumerable<IResponse>> ListStakeholders()
        {
            var response = await Client.GetAsync(ApiUrls.Stakeholders());
            return await response.Content.ReadAsAsync<List<StakeholderResponse>>();
        }

        public async Task<IResponse> DeleteStakeholder(int idStakeholder)
        {
            var response = await Client.DeleteAsync(ApiUrls.Stakeholder(idAction));
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> UpdateStakeholder(int idStakeholder, StakeholderRequest stakeholder)
        {
            var response = await Client.PutAsJsonAsync(ApiUrls.Stakeholder(idStakeholder), stakeholder);
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        #endregion

        #region Action

        public async Task<IResponse> GetAction(int idAction)
        {
            var response = await Client.GetAsync(ApiUrls.Action(idAction));
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<ActionResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> PostAction(ActionRequest action)
        {
            var response = await Client.PostAsJsonAsync(ApiUrls.Actions(), new { action = action });
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadAsAsync<ActionResponse>();
            else
                return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IEnumerable<IResponse>> ListActions()
        {
            var response = await Client.GetAsync(ApiUrls.Actions());
            return await response.Content.ReadAsAsync<List<ActionResponse>>();
        }

        public async Task<IResponse> DeleteAction(int idAction)
        {
            var response = await Client.DeleteAsync(ApiUrls.Stakeholder(idAction));
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        public async Task<IResponse> UpdateAction(int idAction, ActionRequest action)
        {
            var response = await Client.PutAsJsonAsync(ApiUrls.Stakeholder(idAction), action);
            return await response.Content.ReadAsAsync<MessageResponse>();
        }

        #endregion
    }
}
